﻿/*
    March 2011, Muaz Khan, dtweet
    profile settings
*/
(function ( window )
{
    $( window ).load( function ()
    {
        inputValidator();
        changePasswordFunc();
        profileSettingForm();
        showPoshytip();
    } );

    //  jquery.inputTip.js
    function inputValidator()
    {
        $( "#FullName" ).inputTip( {
            goodText: "It seems good...OK",
            badText: "should be a first and last name",
            tipText: "Enter your first and last name",
            validateText: function ( inputValue, callback )
            {
                if ( inputValue.length > 0 ) callback( 1 );
                else callback( 0 );
            },
            validateInRealTime: false
        } );
        $( "#UserName" ).inputTip( {
            goodText: "User Name available...Good!",
            badText: "User Name already had taken",
            tipText: "pick a unique name on dtweet",
            validateText: function ( inputValue, callback )
            {
                $.ajax( {
                    url: domainName + '/Account/CheckUserAvailability',
                    type: 'POST',
                    data: "username=" + inputValue,
                    success: function ( result )
                    {
                        if ( inputValue.length && result.IsSuccess === true )
                        {
                            $( '#nospace' ).html( 'user name <strong>' + result.Message + '</strong> available.' );
                            callback( 1 );
                        }
                        else
                        {
                            $( '#nospace' ).html( 'user name <strong>' + result.Message + '</strong> already had taken.' );
                            callback( 0 );
                        }
                    }
                } );
            },
            validateInRealTime: true
        } );
        $( "#Email" ).inputTip( {
            goodText: "Good email address!",
            badText: "should look like an email address",
            tipText: "we'll get your image by using gravatar",
            validateText: function ( inputValue, callback )
            {
                var emailRegexp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
                if ( emailRegexp.test( inputValue ) ) callback( 1 );
                else callback( 0 );
            },
            validateInRealTime: true
        } );
        $( '#btnSubmit' ).click( function ( e )
        {
            var fullName = $( '#FullName' ).val();
            var userName = $( '#UserName' ).val();
            var email = $( '#Email' ).val();

            if ( fullName.length <= 0 )
            {
                $( '#FullName' ).focus();
                e.preventDefault();
                return;
            }
            if ( userName.length <= 0 )
            {
                $( '#UserName' ).focus();
                e.preventDefault();
                return;
            }
            $.ajax( {
                url: domainName + '/Account/CheckUserAvailability',
                type: 'POST',
                data: "username=" + userName,
                async: false,
                success: function ( result )
                {
                    if ( !result.IsSuccess )
                    {
                        $( '#UserName' ).focus();
                        $( '#nospace' ).html( 'user name <strong>' + result.Message + '</strong> already had taken.' );
                        e.preventDefault();
                        return;
                    }
                }
            } );
            var emailRegexp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            if ( emailRegexp.test( email ) ) { }
            else
            {
                $( '#Email' ).focus();
                e.preventDefault();
                return;
            }
        } );
    }

    function showPoshytip()
    {
        $( '#IsPublic' ).poshytip( {
            className: 'tip-twitter',
            alignTo: 'target',
            alignX: 'center',
            alignY: 'bottom',
            content: function ( updateCallback )
            {
                return "If this is checked, your future tweets will not be available publicly. Tweets posted previously may still be publicly visible in some places.";
            }
        } );
    }

    function changePasswordFunc()
    {
        $( '#changePasswordLink' ).click( function ()
        {
            $( 'div#main' ).block( { message: $( '#changePasswordForm' ), css: { width: '700px',
                'border-bottom-left-radius': '6px 6px',
                'border-bottom-right-radius': '6px 6px',
                'border-top-left-radius': '6px 6px',
                'border-top-right-radius': '6px 6px',
                'box-shadow': '0 0 1px black',
                '-webkit-box-shadow': '0 0 1px black',
                '-moz-box-shadow': '0 0 1px black'
            }
            } );

            $( '#OldPassword' ).focus();

            $( '.close' ).click( function ()
            {
                $( 'div#main' ).unblock();
            } );
        } );
        changePasswordForm();
    }

    function profileSettingForm()
    {
        var profileSettingsOptions = {
            beforeSubmit: function ()
            {
                $( '#main' ).block( {
                    message: '<h1>Processing...</h1>',
                    css: { border: 'none',
                        padding: '15px',
                        backgroundColor: '#000',
                        '-webkit-border-radius': '10px',
                        '-moz-border-radius': '10px',
                        opacity: .5,
                        color: '#fff'
                    }
                } );
            },
            success: function ( result )
            {
                if ( result.IsSuccess == true )
                {
                    $( '#profile-saving-message' ).addClass( 'result-success' ).html( result.Message );
                }
                else
                {
                    $( '#profile-saving-message' ).addClass( 'result-error' ).html( result.Message );
                }
                $( '#main' ).unblock();
                $( '#btnSubmit' ).val( "Save settings" );
            },
            error: function ()
            {
                $( '#profile-saving-message' ).addClass( 'result-error' ).html( 'Something went wrong while updating your profile.' );
                $( '#btnSubmit' ).val( "Save settings" );
                $( '#main' ).unblock();
            }
        };

        $( '#profile-setting-form' ).ajaxForm( profileSettingsOptions );
    }

    function changePasswordForm()
    {
        var passwordOptions = {
            beforeSubmit: function ()
            {
                $( '#changePasswordForm' ).block( {
                    message: '<h1>Processing...</h1>',
                    css: { border: 'none',
                        padding: '15px',
                        backgroundColor: '#000',
                        '-webkit-border-radius': '10px',
                        '-moz-border-radius': '10px',
                        opacity: .5,
                        color: '#fff'
                    }
                } );
            },
            success: function ( result )
            {
                if ( result.IsSuccess == true )
                {
                    $( '#password-changing-message' ).addClass( 'result-success' ).html( result.Message );
                }
                else
                {
                    $( '#password-changing-message' ).addClass( 'result-error' ).html( result.Message );
                }
                $( '#changePasswordForm' ).unblock();
                $( '#btnChangePassword' ).val( "Change your password" );
            },
            error: function ()
            {
                $( '#password-changing-message' ).addClass( 'result-error' ).html( 'Something went wrong while changing your password.' );
                $( '#btnChangePassword' ).val( "Change your password" );
                $( '#changePasswordForm' ).unblock();
            }
        };

        $( '#changePasswordForm' ).ajaxForm( passwordOptions );  // form plugin
    }
} )( window );